package com.project.smarthome.common.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

/**
 * Created by Joker on 2017/2/28.
 */

public class ImgEditText extends EditText {

    private Drawable mLeftDrawable;
    private Drawable mRightDrawable;
    private boolean mHasFoucs;
    private IMyRightDrawableClick mMightDrawableClick;
    private boolean mHide;


    public ImgEditText(Context context) {
        super(context, null);
        init();
    }

    public ImgEditText(Context context, AttributeSet attrs) {
        super(context, attrs, android.R.attr.editTextStyle);
        init();
    }

    public ImgEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Drawable[] drawables = getCompoundDrawables();

        mLeftDrawable = drawables[0];
        mRightDrawable = drawables[2];

        setCompoundDrawablesWithIntrinsicBounds(mLeftDrawable, null, null, null);

        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence text, int start, int before, int count) {
                if (mHasFoucs) {
                    if (text.length() > 0) {
                        setImageVisible(true);
                    } else {
                        setImageVisible(false);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //光标选中时判断  
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        this.mHasFoucs = focused;
        if (focused) {
            setImageVisible(getText().length() > 0);
        } else {
            setImageVisible(false);
        }
    }

    private void setImageVisible(boolean flag) {
        //如果当前右侧有图片则覆盖右侧的图片，如果没有还是显示原来的图片
        if (getCompoundDrawables()[2] != null) {
            mRightDrawable = getCompoundDrawables()[2];
        }
        if (flag) {
            setCompoundDrawables(getCompoundDrawables()[0], null, mRightDrawable, null);
        } else {
            setCompoundDrawables(getCompoundDrawables()[0], null, null, null);
        }
    }


    //设置显示图片的大小
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        setCompoundDrawables(left, top, right, bottom);
    }

    /**
     * 因为我们不能直接给EditText设置点击事件，所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
     * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标，竖直方向就没有考虑
     * （参考 http://blog.csdn.net/xiaanming/article/details/11066685/）
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {

                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) {
                    //调用点击事件（外部实现）
                    mMightDrawableClick.rightDrawableClick();
                }
            }
        }

        return super.onTouchEvent(event);
    }

    //设置右侧按钮的点击事件，外部调用的时候实现该方法
    public void setDrawableClick(IMyRightDrawableClick myMightDrawableClick) {
        this.mMightDrawableClick = myMightDrawableClick;
    }

    public boolean getHide() {
        return mHide;
    }

    public void setHide(boolean hide) {
        mHide = hide;
    }

    //自定义接口（实现右边图片点击事件）
    public interface IMyRightDrawableClick {
        void rightDrawableClick();
    }

    //允许外部修改右侧显示的图片
    public void setmRightDrawable(Drawable drawable) {
        mRightDrawable = drawable;
        setCompoundDrawablesWithIntrinsicBounds(mLeftDrawable, null, mRightDrawable, null);
    }


}
